namespace EFS.EventLog
{
	/// <summary>
	/// Event descriptor
	/// </summary>
	public class EventDescriptor
	{
		// Add information
		public static System.String Info(System.String message, EFS.Common.ISelfDescriptor selfDescriptor)
		{
			return MakeMessage(message, selfDescriptor, "info");
		}

		// Add extended information
		public static System.String Extended(System.String message, EFS.Common.ISelfDescriptor selfDescriptor)
		{
			return MakeMessage(message, selfDescriptor, "extended");
		}

		// Add warning
		public static System.String Warn(System.String message, EFS.Common.ISelfDescriptor selfDescriptor)
		{
			return MakeMessage(message, selfDescriptor, "warn");
		}

		// Add error
		public static System.String Error(System.String message, EFS.Common.ISelfDescriptor selfDescriptor)
		{
			// log
			return MakeMessage(message, selfDescriptor, "err");
		}

		// Add exception
		public static System.String Exception(System.Exception exception, EFS.Common.ISelfDescriptor selfDescriptor)
		{
			// get error
			System.String exDesc = GetExceptionInfo(exception);

			// log
			return MakeMessage(exDesc, selfDescriptor, "except");
		}

		// Log entry
		private static System.String MakeMessage(System.String message, EFS.Common.ISelfDescriptor selfDescriptor, System.String logType)
		{
			// builder
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			// date
			sb.AppendLine(System.DateTime.Now.ToString());

			// log type
			sb.AppendLine(logType);

			// check self
			if (null != selfDescriptor)
			{
				// descriptor
				sb.AppendLine(selfDescriptor.Describe());
			}

			// write
			sb.AppendLine(message);

			// add messages separator
			sb.AppendLine(EFS.EventLog.Config.Log.Separator);

			// close
			return sb.ToString();
		}

		/// Get the exception info
		private static System.String GetExceptionInfo(System.Exception exception)
		{
			// log inner
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			ExceptionInfo(sb, exception);

			// ok
			return sb.ToString();
		}

		/// Get the exception info
		private static void ExceptionInfo(System.Text.StringBuilder sb, System.Exception exception)
		{
			// log inner
			if (null != exception.InnerException)
			{
				sb.AppendLine("Inner ---------------------------------------- ");
				ExceptionInfo(sb, exception.InnerException);
			}

			// set to SB
			sb.AppendFormat("Message: {0}", exception.Message);
			sb.Append(System.Environment.NewLine);
			sb.AppendFormat("Source: {0}", exception.Source);
			sb.Append(System.Environment.NewLine);
			sb.AppendFormat("Site: {0}", exception.TargetSite);
			sb.Append(System.Environment.NewLine);
			sb.AppendFormat("Stack trace: {0} {1}", System.Environment.NewLine, exception.StackTrace);
			sb.Append(System.Environment.NewLine);

			// log
			if (exception is System.Web.Services.Protocols.SoapException)
			{
				System.Web.Services.Protocols.SoapException soapexception =
					(System.Web.Services.Protocols.SoapException)exception;
				sb.AppendFormat(
					"Soap details: {0} {1}",
					System.Environment.NewLine,
					soapexception.Detail.InnerXml);
			}
		}
	}
}